package com.invengo.module_robot.ui.fragment.advert;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.invengo.library_orionstar.RobotConstant;
import com.invengo.library_orionstar.RobotManager;
import com.invengo.module_robot.BR;
import com.invengo.module_robot.R;
import com.invengo.module_robot.databinding.FragmentAdvertBinding;

import java.io.Serializable;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

public class AdvertFragment extends BaseFragment<FragmentAdvertBinding, AdvertViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_advert;
    }
    @Override
    public void onDestroyView() {
        binding.speechView.unRegister();
        super.onDestroyView();
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData() {
        RobotManager.getInstance().changeTip(RobotConstant.TIP_ADVERT);
        binding.viewpager.setPageMargin(DensityUtil.dp2px(getContext(), 14));
        binding.viewpager.setOffscreenPageLimit(3);
        ScalePageTransformer scalePageTransformer = new ScalePageTransformer(false);
        binding.viewpager.setPageTransformer(false, scalePageTransformer);
        viewModel.getAdvert();
    }

    /**
     * 初始视图可观察
     */
    @Override
    public void initViewObservable() {
        viewModel.ui.itemClickEvent.observe(this, bean -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("activity_url", bean.getValue().getUrl());
            NavHostFragment.findNavController(this).navigate(R.id.action_advertFragment2_to_advertInfoFragment,bundle);
        });
        viewModel.ui.changeCurrentEvent.observe(this, position -> {
            binding.viewpager.setCurrentItem(position);
        });
        viewModel.ui.finishLoadDataEvent.observe(this, state -> {
            binding.viewpager.setCurrentItem(state, true);
            initTouchListener(state);
        });
    }

    /**
     * init touch监听器
     *
     * @param position 位置
     */
    private void initTouchListener(int position){
        binding.magicIndicator.attachToViewPager2(binding.viewpager,position);
        binding.viewpager.setOnTouchListener((v, event) -> {
            switch (event.getAction()){
                case MotionEvent.ACTION_MOVE:
                    viewModel.setMoveFinish(false);
                    break;
                case MotionEvent.ACTION_UP:
                    viewModel.setMoveFinish(true);
                    break;
            }
            return false;
        });
    }
}
